﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

    public class EventDispatcher
    {
        private Hashtable EventTable;

        //声明委托
        public delegate void DispatchEventHandler(Object sender, DispatchEventArgs e);
        //声明事件
        public event DispatchEventHandler Dispatch;

        public EventDispatcher()
        {
            EventTable = new Hashtable();
        }

        public void dispacthEvent(int type)
        {
            if (EventTable.ContainsKey(type))
            {
                DispatchEventArgs e = new DispatchEventArgs();
                ArrayList funcArr = (ArrayList)EventTable[type];
                foreach (DispatchEventHandler func in funcArr)
                {
                    this.Dispatch = func;
                    OnDispatch(e);
                }
            }
        }
        public void dispacthEvent(int type, ArrayList data)
        {
            if (EventTable.ContainsKey(type))
            {
                DispatchEventArgs e = new DispatchEventArgs(type,data);
                ArrayList funcArr = (ArrayList)EventTable[type];
                foreach (DispatchEventHandler func in funcArr)
                {
                    this.Dispatch = func;
                    OnDispatch(e);
                }
            }
        }

        // 可以供继承的类重写，以便继承类拒绝其他对象对它的监视
        protected virtual void OnDispatch(DispatchEventArgs e)
        {
            if (Dispatch != null)// 如果有对象注册
            {
                Dispatch(this, e);  // 调用所有注册对象的方法
            }
        }

        /// <summary>
        /// func回调方法格式:(Object obj, DispatchEventArgs e)
        /// </summary>
        /// <param name="type"></param>
        /// <param name="func"></param>
        public void addEventListener(int type, DispatchEventHandler func)
        {
            if (!EventTable.ContainsKey(type))
            {
                ArrayList FuncList = new ArrayList();
                FuncList.Add(func);
                EventTable.Add(type, FuncList);
            }
            else
            {
                ArrayList funclist = (ArrayList)EventTable[type];
                funclist.Add(func);
            }
        }

        public void removeEventListener(int type)
        {
            if (EventTable.ContainsKey(type))
            {
                EventTable.Remove(type);
            }
        }
        public void removeEventListener(int type, DispatchEventHandler func)
        {
            if (EventTable.ContainsKey(type))
            {
                ArrayList funclist = (ArrayList)EventTable[type];
                if (funclist.Contains(func))
                {
                    funclist.Remove(func);
                }
                if (funclist.Count == 0)
                {
                    EventTable.Remove(type);
                }
            }
        }

    }
